package co2.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.ItemSelectEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.chart.PieChartModel;
import org.primefaces.model.tagcloud.DefaultTagCloudItem;
import org.primefaces.model.tagcloud.DefaultTagCloudModel;
import org.primefaces.model.tagcloud.TagCloudItem;
import org.primefaces.model.tagcloud.TagCloudModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.BeanGetter;
import co2.engine.DAOInterface;
import co2.pojos.ChartPojo;
import co2.pojos.Karouzel;
import co2.pojos.Material;


@ManagedBean
@ViewScoped
public class TagCloudBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;


    private TagCloudModel model;
    private PieChartModel pcm;
    private List<Karouzel> karz;
    
    @Autowired
    @Qualifier("daoI")
    private DAOInterface dao;
    
    @PostConstruct
    public void postCon() {
    	createTagCloud();
    	createCarouzelFromMaterial();
    	createPieChart();
    }

    public void shuffle(List list){
    	long seed = System.nanoTime();
		Collections.shuffle(list, new Random(seed));
    }
    
    public void createCarouzelFromMaterial(){
    	karz = new ArrayList<Karouzel>();
    	List<Material> listmat = dao.getAllMaterial();
    	shuffle(listmat);
    	
    	int count=1;
    	
    	if(listmat!=null && listmat.size()>0){
    		
    		if(listmat.size()>3){
    			for(int i=0;i<3;i++){
    				Material m = listmat.get(i);
    				karz.add(new Karouzel(""+count, m.getImage(), m.getTitle(), m.getSumary()));
    	    		count++;
    			}
    		}else{
    			for(Material m:listmat){
    	    		karz.add(new Karouzel(""+count, m.getImage(), m.getTitle(), m.getSumary()));
    	    		count++;
    	    	}
    		}
    	}
    	
    	
    	
    	
    }
    public void createTagCloud(){

        model = new DefaultTagCloudModel();
        model.addTag(new DefaultTagCloudItem("Transformers", 1));
        model.addTag(new DefaultTagCloudItem("MindMap", "/jsf/target/test.jsf", 3));
        model.addTag(new DefaultTagCloudItem("AJAX", 2));
        model.addTag(new DefaultTagCloudItem("jQuery", "/jsf/target/test.jsf", 5));
        model.addTag(new DefaultTagCloudItem("NextGen", 4));
        model.addTag(new DefaultTagCloudItem("JSF 2.0", "/jsf/target/test.jsf", 2));
        model.addTag(new DefaultTagCloudItem("Question", "/jsf/target/multi_choice_question.jsf", 5));
        model.addTag(new DefaultTagCloudItem("Mobile",  3));
        model.addTag(new DefaultTagCloudItem("Themes", "/jsf/target/test.jsf", 4));
        model.addTag(new DefaultTagCloudItem("Rocks", "/jsf/target/test.jsf", 1));
    
    }
    public void createPieChart(){
    	List<ChartPojo> list = dao.getAllChartPojo();
        pcm = new PieChartModel();

        pcm.set("Value 1", 25);
        pcm.set("Value 2", 70);
        pcm.set("Value 3", 100);/*
        for(ChartPojo cp:list){
        	if(!cp.getId().equals("ADMIN")){
        		MstRole mrol = (MstRole) dao.get(MstRole.class, cp.getId());
        		pcm.set(mrol.getRoleName(), Integer.valueOf(cp.getValue()).intValue());
        	}
        }*/
    
    }
    public void itemSelect(ItemSelectEvent event){
    	FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Item selected",
                "Item Index: " + event.getItemIndex() + ", Series Index:" + event.getSeriesIndex());

    		FacesContext.getCurrentInstance().addMessage(null, msg);
    }
    public TagCloudModel getModel() {
        return model;
    }
    
    public void onSelect(SelectEvent event) {
        TagCloudItem item = (TagCloudItem) event.getObject();
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Item Selected", item.getLabel());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

	public PieChartModel getPcm() {
		return pcm;
	}

	public void setPcm(PieChartModel pcm) {
		this.pcm = pcm;
	}

	public List<Karouzel> getKarz() {
		return karz;
	}

	public void setKarz(List<Karouzel> karz) {
		this.karz = karz;
	}
}
